package com.smarttable.ysd;

import java.util.ArrayList;
import java.util.Collections;

import com.smarttable.ysd.adapter.AllAppListViewAdapter;
import com.smarttable.ysd.bean.SortModel;
import com.smarttable.ysd.AppCollectorUtils;
import com.smarttable.ysd.AppManager;
import com.smarttable.ysd.PinyinComparatorUtils;
import com.smarttable.ysd.view.SideBar;
import com.smarttable.ysd.view.SideBar.OnTouchingLetterChangedListener;
import com.smarttable.ysd.view.SideBar;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;

public class AllAppListActivity extends AppCompatActivity {
  
  ListView appListView;
  TextView keywordView;
  SideBar sideBar;
  AllAppListViewAdapter appListViewAdapter;
  ArrayList<SortModel> sortModels;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_list);
    new MyTask().execute("");
  }

  @Override
  protected void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
  }

  @Override
  protected void onPause() {
    super.onPause();
    MobclickAgent.onPause(this);
  }
  
  private void initData() {
    PinyinComparatorUtils pinyinComparator = new PinyinComparatorUtils();
    sortModels = AppCollectorUtils.collect(AppManager.getInstance().getAppList(AllAppListActivity.this));
    // 根据a-z进行排序源数据
    Collections.sort(sortModels, pinyinComparator);
  }
  private void initView(){
    appListView = (ListView)findViewById(R.id.sort_list);
    keywordView = (TextView)findViewById(R.id.keyword);
    sideBar = (SideBar)findViewById(R.id.sidebar);
    
    sideBar.setTextView(keywordView);
    appListViewAdapter = new AllAppListViewAdapter(AllAppListActivity.this, sortModels);
    appListView.setAdapter(appListViewAdapter);
    
    // 设置右侧触摸监听
    sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

        @Override
        public void onTouchingLetterChanged(String s) {
            // 该字母首次出现的位置
            int position = appListViewAdapter.getPositionForSection(s
                    .charAt(0));
            if (position != -1) {
              appListView.setSelection(position);
            }

        }
    });
  }
  class MyTask extends AsyncTask<String, Void, Void>{
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      dialog = new ProgressDialog(AllAppListActivity.this);
      dialog.setMessage(getResources().getString(R.string.all_app_list_loading_text));
      dialog.show();
    }
    @Override
    protected Void doInBackground(String... params) {
      initData();
      return null;
    }
    @Override
    protected void onPostExecute(Void result) {
      // TODO Auto-generated method stub
      super.onPostExecute(result);
      initView();
      dialog.dismiss();
      
    }
    
  }

}
